package com.cn.service.server.PartsManagement.Impl;

import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.bean.vo.PartsManagement.BusinessProjectTypeMapping;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.PartsManagement.BusinessProjectTypeMappingDao;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.PartsManagement.BusinessDepartmentTypeService;
import com.cn.service.server.PartsManagement.BusinessProjectTypeMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BusinessProjectTypeMappingServiceImpl implements BusinessProjectTypeMappingService {
    private final BusinessProjectTypeMappingDao businessProjectTypeMappingDao;
    private final Sys_Sequence_Info_TDAO sysSequenceInfoTdao;

    @Autowired(required = false)
    public BusinessProjectTypeMappingServiceImpl(BusinessProjectTypeMappingDao businessProjectTypeMappingDao, Sys_Sequence_Info_TDAO sysSequenceInfoTdao) {
        this.businessProjectTypeMappingDao = businessProjectTypeMappingDao;
        this.sysSequenceInfoTdao = sysSequenceInfoTdao;
    }


    @Override
    public List<BusinessProjectTypeMapping> selectBusinessProjectTypeMapping(int page, int limit, String name) {
        return businessProjectTypeMappingDao.selectBusinessProjectTypeMapping(page, limit, name);
    }

    @Override
    public Result addBusinessProjectTypeMapping(int businessTypeID, int itemType, int businessProjectID, String description, int status) {
        List<BusinessProjectTypeMapping> businessProjectTypeMappings = businessProjectTypeMappingDao.selectAll();
        for (BusinessProjectTypeMapping bus : businessProjectTypeMappings) {
            if (bus.getBusinessTypeID() == businessTypeID && bus.getBusinessProjectID() ==businessProjectID&&bus.getItemType() == itemType) {
                return Result.failure("500", "该业务类型已经被使用,请选择其它业务类型");
            }
        }
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("BusinessTeamPersonnelInfoTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "BusinessTeamPersonnelInfoTable");
        int i = businessProjectTypeMappingDao.addBusinessProjectTypeMapping(ID, businessTypeID, itemType, businessProjectID, description, status);


        if (i == 0) {
            return Result.failure("500", "添加失败");
        }
        return Result.success("200", "添加成功");
    }

    @Override
    public Result deleteBusinessProjectTypeMapping(List<BusinessProjectTypeMapping> idList) {
        for (BusinessProjectTypeMapping bus : idList) {
            Integer integer = businessProjectTypeMappingDao.selectCountBusinessProjectTypeMapping(bus.getID());
            if (integer>0){
                return Result.failure("500", "业务项目与业务类型对照已引用这条数据，不能删除");
            }
            Integer integer1 = businessProjectTypeMappingDao.selectCountEquipmentStructureMain(bus.getID());
            if (integer1>0){
                return Result.failure("500", "设备结构信息已引用这条数据，不能删除");
            }
        }
        for (BusinessProjectTypeMapping bus : idList) {
            int i = businessProjectTypeMappingDao.deleteBusinessProjectTypeMapping(bus.getID());
            if (i == 0) {
                return Result.failure("500", "删除失败");

            }
        }
        return Result.success("200", "删除成功");
    }

    @Override
    public Result upDateBusinessProjectTypeMapping(int ID, int businessTypeID, int status, int itemType, int businessProjectID, String description) {
        List<BusinessProjectTypeMapping> businessProjectTypeMappings = businessProjectTypeMappingDao.selectAll();
        for (BusinessProjectTypeMapping bus : businessProjectTypeMappings) {
            if (bus.getBusinessTypeID() == businessTypeID && bus.getID() != ID && bus.getBusinessProjectID() ==businessProjectID&&bus.getItemType() == itemType) {
                return Result.failure("500", "该业务类型已经被使用,请选择其它业务类型");
            }
        }
        int i = businessProjectTypeMappingDao.upDateBusinessProjectTypeMapping(ID, businessTypeID, status, itemType, businessProjectID, description);
        if (i == 0) {
            return Result.failure("500", "修改失败");
        }
        return Result.success("200", "修改成功");
    }
}
